<?php
require_once 'models/models_mathang.php';
$mod = $_GET['mod'];
$obmh = new models_mathang();
require_once 'models/models_loaihang.php'; //goi trang model loai hang
$oblh = new models_loaihang(); //khoi tao doi tuong loai hang
$p=new pagers();//khoi tao doi tuong pager
require_once 'models/models_chitietnhap.php';
$ctn=new models_chitietnhap();
require_once 'models/models_cthoadon.php';
$cthd=new models_cthoadon();
switch ($mod){
	case 'mathang':{
		$limit=7;
		$count=$obmh->tongsodong();
		$start=$p->findstart($limit);
		$page=$p->findpages($count,$limit);
		$data['list'] = $obmh->danhsachmh($start, $limit);
		$pagelist=$p->pagelist($_GET['page'],$page);
		$obmh = new models_mathang();
		$data['listlh'] = $oblh->listLoaiHang();
		
		include_once 'views/admin/mathang/index.php';
		break;
	}
	case 'addmathang':{
		if($_POST){
		if (isset($_FILES))
					{
						move_uploaded_file($_FILES['hinhanh']['tmp_name'], 'public/upload/'.$_FILES['hinhanh']['name']);
						$HinhAnh = $_FILES['hinhanh']['name'];
					}
			
			$obmh->addMatHang($_POST['cmb_choice'], $_POST['tenmh'], $_POST['dvt'], $_POST['soluong'], $_POST['gia'], $HinhAnh , $_POST['mota'],$_POST['anhien']);
			echo '<script> window.location.href="admin.php?mod=mathang"</script>';
		}
		$data['list'] = $oblh->listloaihang(); //lay danh sach loai hang
		require 'views/admin/mathang/add.php';
		break;
	}
	case 'delmathang':{
		$obmh->delMatHang($_GET['ma']);
		echo '<script> window.location.href="admin.php?mod=mathang"</script>';
		break;
	}

	case 'updatemathang':{
		if($_POST){
		if (isset($_FILES))
					{
						move_uploaded_file($_FILES['hinhanh']['tmp_name'], 'public/upload/'.$_FILES['hinhanh']['name']);
						$HinhAnh = $_FILES['hinhanh']['name'];
					}
		$obmh->updateMatHang($_POST['cmb_choice'], $_POST['tenmh'], $HinhAnh, $_POST['gia'], $_POST['soluong'], $_POST['dvt'], $_POST['mota'], $_POST['anhien'],$_GET['ma']);
				echo '<script>window.location.href="admin.php?mod=mathang"</script>';
			}
		$data = $obmh->getMatHang($_GET['ma']);
		$data['listlh'] = $oblh->listloaihang();
		require 'views/admin/mathang/edit.php';
		break;
	}
	default: echo 'yeu cau chua duoc xu ly !'; break;
}
?>